﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_LayoutIndex.cshtml";
}@using LeSoft.Application.Code;
<script>
    var keyValue = request('keyValue');
    var _organizeid = "@LeSoft.Application.Code.OperatorProvider.Provider.Current().CompanyId"
    $(function () {
        initControl();
        GetGrid()
    })
    //初始化控件
    function initControl() {
        var Mode = "@ViewBag.Mode";
        if (Mode == "True") {
            $("#Code").attr("placeholder", "系统已启用自动编号").attr("disabled", "disabled");
        } else {
            $("#Code").attr("isvalid", "yes").attr("checkexpession", "NotNull").attr("placeholder", "请输入编号");
        }
        //组织机构
        $("#OrganizeId").ComboBoxTree({
            url: "../../BaseManage/Organize/GetTreeJsonById",
            height: "250px",
            description: "===选择组织机构===",
            click: function (item) {
                _organizeid = item.id;
            }
        });
        $("#OrganizeId").ComboBoxSetValue(_organizeid);
        if (!!keyValue) {
            $.SetForm({
                url: "../../ResourceManage/RS_PStructUser/GetLiveInBillInfo",
                param: { keyValue: keyValue },
                success: function (data) {

                }
            });
        }
    }


    //添加
    function btn_add() {

        var obj = $("#gridTable").jqGrid("getRowData");
        dialogOpen({
            id: "AddPStructForm",
            title: '添加入住房间',
            url: '../ResourceManage/RS_PStructUser/AddPStructForm',
            width: "900px",
            height: "600px",
            callBack: function (iframeId) {
                top.frames[iframeId].AcceptClick(function (data) {
                    var $gridTable = $("#gridTable");
                    var GridData = $gridTable.jqGrid("getRowData");
                    for (var i = 0; i < GridData.length; i++) {
                        var data = $.grep(data, function (value) {
                            return value.id != GridData[i].id;
                        });
                    }
                    $gridTable.addRowData(($gridTable.jqGrid("getRowData").length + 1), data);
                });
            }
        });
    };

    //移除
    function btn_delete_field() {
        var keyValue = $("#gridTable").getGridParam('selrow');
        if (keyValue) {
            dialogConfirm('注：您确定要移除吗？该操作将无法恢复？', function (r) {
                if (r) {
                    $("#gridTable").delRowData(keyValue);
                    dialogMsg("移除成功。", 1);
                }
            });

        } else {
            dialogMsg('请选择需要移除的数据！', 0);
        }
    }
    //保存
    function AcceptClick() {
        if (!$('#Billfrom').Validform()) {
            return false;
        }
        var postData = $("#Billfrom").GetWebControls(keyValue);
        var $gridTable = $("#gridTable");
        var obj = $gridTable.jqGrid("getRowData");
        if (obj.length == 0) {
            dialogMsg('未选择入住房间！', 0);
            return false
        }
        postData["CustomerId"] = $("#CustomerName").attr("title");
        postData["RoomList"] = JSON.stringify(obj);
        $.SaveForm({
            url: "../../ResourceManage/RS_PStructUser/SaveLiveForm",
            param: { queryJson: JSON.stringify(postData) },
            loading: "正在保存数据...",
            success: function () {
                $.currentIframe().$("#gridTable").resetSelection();
                $.currentIframe().$("#gridTable").trigger("reloadGrid");
            }
        })

    };

    function GetDataSelect(Type) {
        var options = {};
        switch (Type) {
            case 1:
                options["type"] = 1;
                options["data"] = [{ "id": "CustomerName" }, { "id": "CustomerCode" }];
                options["organizeid"] = '@LeSoft.Application.Code.OperatorProvider.Provider.Current().CompanyId';
                break;
            default:

        }
        DataItemClick(JSON.stringify(options));
    };
    //房间列表
    function GetGrid() {
        var $gridTable = $("#gridTable");

        $gridTable.jqGrid({
            datatype: "json",
            height: 350,
            autowidth: true,
            unwritten: false,
            colModel: [
             { label: "主键", name: "id", hidden: true },
             { label: "room主键", name: "roomid", hidden: true },
             { label: "房屋名称", name: "name", index: "name", width: 280, align: "center", sortable: false },
             { label: "房屋编号", name: "code", index: "code", width: 180, align: "center", sortable: false },
             { label: "建筑面积", name: "area", index: "area", width: 100, align: "center", sortable: false },
             { label: "房间描述", name: "memo", index: "memo", width: 260, align: "center", sortable: false },
            ],
            caption: "房间列表",
            rowNum: 100,
            rownumbers: true,
            shrinkToFit: false,
            gridview: true,
            hidegrid: false,
            gridview: true,
            subGrid: true,
            subGridRowExpanded: function (subgrid_id, row_id) {
                var PStructId = $gridTable.getCell(row_id, "id");
                var subgrid_table_id = subgrid_id + "_t";
                var subhtml = "";
                subhtml += '<div><table id="' + subgrid_table_id + '"></table></table></div>';
                $("#" + subgrid_id).html(subhtml);
                $("#" + subgrid_table_id).jqGrid({
                    caption: "房屋物品列表",
                    hidegrid: false,
                    datatype: "json",
                    url: "../../ResourceManage/RS_PStructPublish/GetRoomDetial?KeyValue=" + PStructId + "&IsSubmit=F",
                    height: 170,
                    colModel: [
                     { label: "Id", name: "Id", hidden: true },
                     { label: "物品名称", name: "Name", width: 160, align: "left", sortable: false },
                     { label: "物品编号", name: "Code", width: 100, align: "left", sortable: false },
                     { label: "所属部位", name: "PSMemo", width: 90, align: "left", sortable: false },
                     { label: "规格型号", name: "ModelNo", width: 80, align: "left", sortable: false },
                     { label: "数量", name: "Num", width: 80, align: "left", sortable: false },
                     { label: "赔偿单价", name: "UnitPrice", width: 80, align: "left", sortable: false },
                     { label: "备注", name: "Memo", width: 80, align: "left", sortable: false },
                    ],
                    gridComplete: function () {
                        $("#" + subgrid_table_id).closest("div.ui-jqgrid-view").children("div.ui-jqgrid-titlebar").css("text-align", "left");
                    }
                });
            },
        });

    }
</script>
<div style="width:98%;">
    <table class="form" id="Billfrom" style="margin:0px auto;">
        <tr>
            <td class="formTitle">所属机构<font face="宋体">*</font></td>
            <td class="formValue" colspan="2">
                <div id="OrganizeId" type="selectTree" class="ui-select BaseClass" isvalid="yes" checkexpession="NotNull"></div>
            </td>
            <td class="formTitle">单据编号</td>
            <td class="formValue" colspan="2">
                <input id="Code" type="text" placeholder="系统自动生成" class="form-control " />

            </td>
        </tr>
        <tr>
            <td class="formTitle">入住时间</td>
            <td class="formValue" colspan="2">
                <input id="BeginDate" type="text" class="form-control input-wdatepicker  " placeholder="请输入入住时间" value="@LeSoft.Util.Time.GetToday("yyyy-MM-dd HH:mm")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd  HH:mm' })" />
            </td>
            <td class="formTitle">退住时间</td>
            <td class="formValue" colspan="2">
                <input id="EndDate" type="text" class="form-control input-wdatepicker BaseClass " placeholder="请输入退住时间" value="@LeSoft.Util.Time.GetToday("yyyy-MM-dd HH:mm")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd  HH:mm' })" />
            </td>
        </tr>
        <tr>
            <td class="formTitle">租户名称</td>
            <td class="formValue" colspan="2">
                <div class="input-group" style="width: 100%;">
                    <input id="CustomerName" type="text" class="form-control">
                    <span class="input-group-btn">
                        <a onclick="GetDataSelect(1)" class="form-control" style="cursor: pointer"><i class="fa fa fa-book BaseClass  " style=" font-size: 20px; padding-right: 5px;   color: gray"></i></a>
                    </span>
                </div>
            </td>
            <td class="formTitle">租户编号</td>
            <td class="formValue" colspan="2">
                <input id="CustomerCode" type="text" class="form-control " />
            </td>
        </tr>
        <tr>
            <th class="formTitle">
                说明
            </th>
            <td class="formValue" colspan="5">
                <textarea id="Description" class="form-control " style="height: 80px;"></textarea>
            </td>
        </tr>
    </table>
</div>
<div style="margin: 5px; margin-top: 0px; margin-bottom: 0px; width:98%;">
    <table id="gridTable"></table>
    <style>
        .ui-jqgrid .ui-jqgrid-caption {
            text-align: right;
            padding-right: 5px;
            background-color: #F5F5F5;
        }
    </style>
</div>
<div class="toolbar" style="position: absolute; top: 195px; left: 10px;">
    <a id="lr-addField" class="btn btn-success btn-xs BaseClass" onclick="btn_add()"><i class="fa fa-plus BaseClass"></i>&nbsp;绑定房间</a>
    <a id="lr-deleteField" class="btn btn-danger btn-xs BaseClass" onclick="btn_delete_field()"><i class="fa fa-trash-o BaseClass"></i>&nbsp;移除房间</a>
</div>




